package cn.mumzone.pos.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

import butterknife.BindView;
import butterknife.OnClick;
import cn.mumzone.basic.base.BaseActivity;
import cn.mumzone.basic.dialog.AlertDialog;
import cn.mumzone.basic.loading.button.LoadingButton;
import cn.mumzone.basic.util.DisplayUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.ToastUtil;
import cn.mumzone.basic.util.log.Elog;
import cn.mumzone.pos.R;
import cn.mumzone.pos.dialog.PosDialog;
import cn.mumzone.pos.presenter.LoginPresenter;
import cn.mumzone.pos.presenter.contract.LoginContract;
import retrofit2.http.POST;

/**
 * Created by qiuzhenhuan on 2019/6/24.
 */
public class ACT_Login extends BaseActivity<LoginPresenter> implements LoginContract.View {

    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn_login)
    LoadingButton btnLogin;

    private AlertDialog changeAddressDialog;

    @OnClick({R.id.iv_change_address, R.id.iv_logo, R.id.btn_login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_change_address:
                mPresenter.onClickAddress();
                break;
            case R.id.iv_logo:
                mPresenter.onClickCount();
                break;
            case R.id.btn_login:
                String phone = etPhone.getText().toString().trim();
                String pwd = etPwd.getText().toString();
                btnLogin.startLoading("正在登录", etPhone, etPwd);
                mPresenter.getUserInfo(phone, pwd);
                break;
        }

    }

    @Override
    protected void initEventAndData() {
        etPhone.clearFocus();
        etPwd.clearFocus();
        initChangeAddressDialog();
    }

    private void initChangeAddressDialog() {
        AlertDialog.Builder.OnSingleConfirmListener onSingleConfirmListener = new AlertDialog.Builder.OnSingleConfirmListener() {
            @Override
            public void onConfirmClicked(AlertDialog dialog) {
                EditText etAddress = changeAddressDialog.getView(R.id.et_ip_address);
                EditText etPosCode = changeAddressDialog.getView(R.id.et_pos_code);
                String address = etAddress.getText().toString().trim();
                String posCode = etPosCode.getText().toString().trim();
                mPresenter.changeAddress(address, posCode);
                dialog.dismiss();
            }
        };

        changeAddressDialog = new PosDialog.PosBuilder(mContext)
                .setRealDialogContent(R.layout.dialog_change_address)
                .setDialogTitle("切换环境")
                .setSingleConfirmText("确认更改")
                .setWidthAndHeight(386, 300)
                .setOnSingleConfirmListener(onSingleConfirmListener)
                .create();
    }

    @Override
    protected int getLayout() {
        return R.layout.act_login;
    }

    @Override
    public void onUserLogin() {
        if(mPresenter.isAllDataReady()){
            btnLogin.stopLoading("登录");
            ACT_Home.startAc(mContext);
            finish();
        }
    }


    @Override
    public void onDebugModeOpened() {
        PosToast.INSTANCE.makeToast(mContext, PosToast.ToastType.TOAST_TYPE_SQUARE,
                PosToast.ToastSquareLevel.ALERT_FAIL_SQUARE, "已进入debug模式", false);
    }

    @Override
    public void showChangeAddressDialog() {

        if (changeAddressDialog != null) {
            changeAddressDialog.show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeDialog(changeAddressDialog);
    }

    @Override
    public void showError(String msg) {
        super.showError(msg);
        btnLogin.stopLoading("登录");
    }


    public static void startAc(Context context) {
        Intent intent = new Intent(context, ACT_Login.class);
        context.startActivity(intent);
    }
}
